home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_217 / stevie / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  350 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #ifdef AMIGA
  10. # include <proto/exec.h>
  11. #endif
  12.  
  13. #include "stevie.h"
  14.  
  15. int             Rows;        /* Number of Rows and Columns */
  16. int             Columns;    /* in the current window. */
  17.  
  18. int             CheckTopcharAndBotchar = FALSE;
  19. int             MustUpdateBotchar = FALSE;
  20. int             ValidToCurschar = FALSE;
  21. int             LineNotValid = FALSE;
  22.  
  23. int             NumLineSizes = -1;    /* # of active LineSizes */
  24. LINE          **LinePointers = NULL;    /* Pointer to the line for LineSizes */
  25. char           *LineSizes = NULL;    /* Size of a line (pline output) */
  26.  
  27. char           *Filename = NULL;/* Current file name */
  28.  
  29. LPtr           *Filemem;    /* The contents of the file, as a single
  30.                  * array. */
  31. LPtr           *Filetop;    /* Line 'above' the start of the file */
  32.  
  33. LPtr           *Fileend;    /* Pointer to the end of the file in Filemem.
  34.                  * (It points to the byte AFTER the last
  35.                  * byte.) */
  36.  
  37. LPtr           *Topchar;    /* Pointer to the byte in Filemem which is in
  38.                  * the upper left corner of the screen. */
  39.  
  40. LPtr           *Botchar;    /* Pointer to the byte in Filemem which is
  41.                  * just off the bottom of the screen. */
  42.  
  43. LPtr           *Curschar;    /* Pointer to byte in Filemem at which the
  44.                  * cursor is currently placed. */
  45.  
  46. int             Curscol;    /* Current position of cursor (column) */
  47. int             Cursrow;    /* Current position of cursor (row) */
  48.  
  49. int             Cursvcol;    /* Current virtual column, the column number
  50.                  * of the file's actual line, as opposed to
  51.                  * the column number we're at on the screen.
  52.                  * This makes a difference on lines that span
  53.                  * more than one screen line. */
  54.  
  55. int             Curswant = 0;    /* The column we'd like to be at. This is
  56.                  * used try to stay in the same column
  57.                  * through up/down cursor motions. */
  58.  
  59. bool_t          set_want_col;    /* If set, then update Curswant the next time
  60.                  * through cursupdate() to the current
  61.                  * virtual column. */
  62.  
  63. int             State = NORMAL;    /* This is the current state of the command
  64.                  * interpreter. */
  65.  
  66. int             Prenum = 0;    /* The (optional) number before a command. */
  67.  
  68. LPtr           *Insstart;    /* This is where the latest insert/append
  69.                  * mode started. */
  70.  
  71. bool_t          Changed = FALSE;/* Set to TRUE if something in the file has
  72.                  * been changed and not written out. */
  73.  
  74. char           *IObuff;        /* file reads are done, one line at a time,
  75.                  * into this buffer; as well as sprintf's */
  76.  
  77. char           *Insbuffptr = NULL;
  78. char           *Insbuff;    /* Each insertion gets stuffed into this
  79.                  * buffer. */
  80.  
  81. char           *Readbuffptr = NULL;
  82. char           *Readbuff;    /* Having this buffer allows STEVIE to easily
  83.                  * make itself do commands */
  84.  
  85. char           *Redobuffptr = NULL;
  86. char           *Redobuff;    /* Each command should stuff characters into
  87.                  * this buffer that will re-execute itself. */
  88.  
  89. bool_t          UndoInProgress = FALSE;    /* Set to TRUE if undo'ing */
  90. char           *Undobuffptr = NULL;
  91. char           *Undobuff;    /* Each command should stuff characters into
  92.                  * this buffer that will undo its effects. */
  93.  
  94. char           *UndoUndobuffptr = NULL;
  95. char           *UndoUndobuff;    /* Each command should stuff characters into
  96.                  * this buffer that will undo its undo. */
  97.  
  98. char           *Yankbuffptr = NULL;
  99. char           *Yankbuff;    /* Yank buffer */
  100.  
  101. char            last_command = NUL;    /* last command */
  102. char            last_command_char = NUL;    /* character needed to undo
  103.                          * last command */
  104.  
  105. bool_t          RedrawingDisabled = FALSE;    /* Set to TRUE if undo'ing or
  106.                          * put'ing */
  107.  
  108. char          **files;        /* list of input files */
  109. int             numfiles;    /* number of input files */
  110. int             curfile;    /* number of the current file */
  111.  
  112. static void
  113. usage()
  114. {
  115.     fprintf(stderr, "usage: stevie [file ...]\n");
  116.     fprintf(stderr, "       stevie -t tag\n");
  117.     fprintf(stderr, "       stevie +[num] file\n");
  118.     fprintf(stderr, "       stevie +/pat  file\n");
  119.     exit(1);
  120. }
  121.  
  122. #ifdef AMIGA
  123. void
  124. #else
  125. int
  126. #endif
  127. main(argc, argv)
  128.     int             argc;
  129.     char          **argv;
  130. {
  131.     char           *initstr;    /* init string from the environment */
  132.     char           *tag = NULL;    /* tag from command line */
  133.     char           *pat = NULL;    /* pattern from command line */
  134.     int             line = -1;    /* line number from command line */
  135.  
  136.     int             atoi();
  137.     char           *getenv();
  138.  
  139. #ifdef AMIGA
  140.     {
  141.     struct Library *DosBase;/* Used for checking version */
  142.  
  143.     DosBase = OpenLibrary("dos.library", 33);
  144.     if (!DosBase) {
  145.         fprintf(stderr,
  146.          "\nSTEVIE requires Version 33 or later of dos.library.\n");
  147.         exit(2);
  148.     } else {
  149.         CloseLibrary(DosBase);
  150.     }
  151.  
  152. /*
  153.  * I don't think STEVIE should be exited with a break.
  154.  */
  155.     (void) signal(SIGINT, SIG_IGN);
  156.     }
  157. #endif
  158.  
  159.     /*
  160.      * Process the command line arguments. 
  161.      */
  162.     if (argc > 1) {
  163.     switch (argv[1][0]) {
  164.  
  165.       case '-':        /* -t tag */
  166.         if (argv[1][1] != 't')
  167.         usage();
  168.  
  169.         if (argv[2] == NULL)
  170.         usage();
  171.  
  172.         Filename = NULL;
  173.         tag = argv[2];
  174.         numfiles = 1;
  175.         break;
  176.  
  177.       case '+':        /* +n or +/pat */
  178.         if (argv[1][1] == '/') {
  179.         if (argv[2] == NULL)
  180.             usage();
  181.         Filename = strsave(argv[2]);
  182.         pat = &(argv[1][1]);
  183.         numfiles = 1;
  184.  
  185.         } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  186.         if (argv[2] == NULL)
  187.             usage();
  188.         Filename = strsave(argv[2]);
  189.         numfiles = 1;
  190.  
  191.         line = (isdigit(argv[1][1])) ?
  192.             atoi(&(argv[1][1])) : 0;
  193.         } else
  194.         usage();
  195.  
  196.         break;
  197.  
  198.       default:        /* must be a file name */
  199.         Filename = strsave(argv[1]);
  200.         files = &(argv[1]);
  201.         numfiles = argc - 1;
  202.         break;
  203.     }
  204.     } else {
  205.     Filename = NULL;
  206.     numfiles = 1;
  207.     }
  208.     curfile = 0;
  209.  
  210.     windinit();
  211.  
  212.     /*
  213.      * Allocate LPtr structures for all the various position pointers and for
  214.      * the many buffers. 
  215.      */
  216.     Filetop = (LPtr *) alloc((unsigned) sizeof(LPtr));
  217.     Filemem = (LPtr *) alloc((unsigned) sizeof(LPtr));
  218.     Fileend = (LPtr *) alloc((unsigned) sizeof(LPtr));
  219.     Topchar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  220.     Curschar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  221.     Botchar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  222.     Insstart = (LPtr *) alloc((unsigned) sizeof(LPtr));
  223.     IObuff = alloc(IOSIZE);
  224.     Insbuff = alloc(INSERT_SIZE);
  225.     Readbuff = alloc(READSIZE);
  226.     Redobuff = alloc(REDO_UNDO_SIZE);
  227.     Undobuff = alloc(REDO_UNDO_SIZE);
  228.     UndoUndobuff = alloc(REDO_UNDO_SIZE);
  229.     Yankbuff = alloc(YANKSIZE);
  230.     if (Filetop == NULL ||
  231.     Filemem == NULL ||
  232.     Fileend == NULL ||
  233.     Topchar == NULL ||
  234.     Curschar == NULL ||
  235.     Botchar == NULL ||
  236.     Insstart == NULL ||
  237.     IObuff == NULL ||
  238.     Insbuff == NULL ||
  239.     Readbuff == NULL ||
  240.     Redobuff == NULL ||
  241.     Undobuff == NULL ||
  242.     UndoUndobuff == NULL ||
  243.     Yankbuff == NULL) {
  244.     fprintf(stderr, "Can't allocate data structures\n");
  245.     windexit(0);
  246.     }
  247.     screenalloc();
  248.     filealloc();        /* Initialize Filemem, Filetop & Fileend */
  249.  
  250.     s_clear();
  251.  
  252.     initstr = getenv("EXINIT");
  253.     if (initstr != NULL) {
  254.     char           *lp, buf[128];
  255.  
  256.     lp = getenv("LINES");
  257.     if (lp != NULL) {
  258.         sprintf(buf, "%s lines=%s", initstr, lp);
  259.         readcmdline(':', buf);
  260.     } else
  261.         readcmdline(':', initstr);
  262.     }
  263.     if (Filename != NULL) {
  264.     if (readfile(Filename, Filemem, FALSE))
  265.         filemess("[New File]");
  266.     } else
  267.     msg("Empty Buffer");
  268.  
  269.     setpcmark();
  270.  
  271.     if (tag) {
  272.     stuffReadbuff(":ta ");
  273.     stuffReadbuff(tag);
  274.     stuffReadbuff("\n");
  275.     } else if (pat) {
  276.     stuffReadbuff(pat);
  277.     stuffReadbuff("\n");
  278.     } else if (line >= 0) {
  279.     if (line > 0)
  280.         stuffnumReadbuff(line);
  281.     stuffReadbuff("G");
  282.     }
  283.     edit();
  284.     /* NOTREACHED */
  285.     /* windexit(0); */
  286. }
  287.  
  288. void
  289. stuffReadbuff(s)
  290.     char           *s;
  291. {
  292.     if (Readbuffptr == NULL) {
  293.     if ((strlen(s) + 1) < READSIZE) {
  294.         strcpy(Readbuff, s);
  295.         Readbuffptr = Readbuff;
  296.         return;
  297.     }
  298.     } else if ((strlen(Readbuff) + (strlen(s) + 1)) < READSIZE) {
  299.     strcat(Readbuff, s);
  300.     return;
  301.     }
  302.     emsg("Couldn't stuffReadbuff() - clearing Readbuff\n");
  303.     *Readbuff = NUL;
  304.     Readbuffptr = NULL;
  305. }
  306.  
  307. void
  308. stuffnumReadbuff(n)
  309.     int             n;
  310. {
  311.     char            buf[32];
  312.  
  313.     sprintf(buf, "%d", n);
  314.     stuffReadbuff(buf);
  315. }
  316.  
  317. /* OPTRESULT */
  318. char
  319. vgetc()
  320. {
  321.     int             c;
  322.  
  323.     /*
  324.      * inchar() may map special keys by using stuffReadbuff(). If it does so,
  325.      * it returns -1 so we know to loop here to get a real char. 
  326.      */
  327.     do {
  328.     if (Readbuffptr != NULL) {
  329.         char            nextc = *Readbuffptr++;
  330.  
  331.         if (*Readbuffptr == NUL) {
  332.         *Readbuff = NUL;
  333.         Readbuffptr = NULL;
  334.         }
  335.         return (nextc);
  336.     }
  337.     c = inchar();
  338.     } while (c == -1);
  339.  
  340.     return (char) c;
  341. }
  342.  
  343. char
  344. vpeekc()
  345. {
  346.     if (Readbuffptr != NULL)
  347.     return (*Readbuffptr);
  348.     return (NUL);
  349. }
  350.